import { addCommunity, deleteCommunity, getCommunityPage } from '../services/api';

export default {
  namespace: 'community',
  state: {
    communityList: [],
  },
  effects: {
    *addCommunity({ payload, callback }, { call, put }) {
      yield call(addCommunity, payload);
      if (callback) callback();
    },
    *getCommunityList({ payload }, { call, put }) {
      const response = yield call(getCommunityPage, payload);
      yield put({
        type: 'saveCommunityList',
        payload: response,
      });
    },
    *deleteCommunity({ payload, callback }, { call, put }) {
      yield call(deleteCommunity, payload);
      if (callback) callback();
    },
  },
  reducers: {
    saveCommunityList(state, action) {
      return {
        ...state,
        communityList: action.payload.data,
      };
    },
  },
};
